package cn.shyouchi.wuguanjia.android.ui.QRCode;

import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

import static cn.shyouchi.wuguanjia.android.R.id.topBar;

public class QRCodeActivity extends BaseActivity implements QRCodeView.Delegate {


    @BindView(topBar)
    TopBar mTopBar;
    @BindView(R.id.zxingview)
    ZXingView mZxingview;

    //    private static final String TAG = TestScanActivity.class.getSimpleName();
    private static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;


    @Override
    public int initContentView() {
        return R.layout.activity_qrcode;
    }

    @Override
    public void initInjector() {

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle("二维码扫描");
        setSupportActionBar(mTopBar.getToolbar());
        mZxingview.setDelegate(this);
        mZxingview.startSpotAndShowRect();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    public void onScanQRCodeSuccess(String result) {

        LogUtil.e(result);

        vibrate();
//        Uri uri = Uri.parse(result);
        QRCodeParser.instance(this, result).parse();
//        LogUtil.e(String.format("%s %s %s ", uri.getScheme(),
//        uri.getHost(),
//        uri.getQueryParameter("meetId")));

        finish();


//        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();

        mZxingview.startSpot();
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mZxingview.startCamera();
    }

    @Override
    protected void onDestroy() {
        mZxingview.onDestroy();
        super.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mZxingview.showScanRect();


//        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY) {
//            final String picturePath = BGAPhotoPickerActivity.getSelectedImages(data).get(0);
//            new AsyncTask<Void, Void, String>() {
//                @Override
//                protected String doInBackground(Void... params) {
//                    return QRCodeDecoder.syncDecodeQRCode(picturePath);
//                }
//
//                @Override
//                protected void onPostExecute(String result) {
//                    if (TextUtils.isEmpty(result)) {
//                        Toast.makeText(mContext, "未发现二维码", Toast.LENGTH_SHORT).show();
//                    } else {
//                        Toast.makeText(mContext, result, Toast.LENGTH_SHORT).show();
//                    }
//                }
//            }.execute();
//        }
    }


}
